<?php
/**
 * Fasade class for the price modules
 *
 * @access 	public
 * @author 	Valentin
 * @copyright	ResMania 2009 all rights reserved.
 * @version	1.0
 * @link		http://developer.resmania.com/api
 * @since  	05-2009
 */
class RM_Module_HourlyPrices extends RM_Module implements RM_Prices_Interface, RM_Unit_Copy_Interface
{    
    function copyInformation(RM_Unit_Row $original, RM_Unit_Row $copy)
    {
        $unitPricesModel = new RM_HourlyUnitPrices();
        $unitPrices = $unitPricesModel->getByUnit($original);
        foreach ($unitPrices as $unitPrice) {
            $unitPriceCopyData = $unitPrice->toArray();
            unset($unitPriceCopyData['id']);
            $unitPriceCopyData['unit_id'] = $copy->id;
            $unitPriceCopy = $unitPricesModel->createRow($unitPriceCopyData);
            $unitPriceCopy->save();
        }

        $unitPricesConfigModel = new RM_UnitHourlyPricesConfig();
        $unitPricesConfigs = $unitPricesConfigModel->getByUnit($original);
        foreach ($unitPricesConfigs as $unitPricesConfig) {
            $unitPricesConfigCopyData = $unitPricesConfig->toArray();
            $unitPricesConfigCopyData['unit_id'] = $copy->id;
            $unitPricesConfigCopy = $unitPricesConfigModel->createRow($unitPricesConfigCopyData);
            $unitPricesConfigCopy->save();
        }
    }

    public function  __construct() {
        $this->name = 'HourlyPrices';
    }       

    public function getNode(){
       return null;
    }    

    /**
     * Returns PHP dateformat to present reservation start and end date on the UI
     *
     * @param bool $php - internal PHP date format or UI date format, if true - internal PHP date format
     * @return string
     */
    public function getDateformat($php = false){
        if ($php) {
            return RM_Config::MYSQL_DATEFORMAT;
        }
        $config = new RM_Config();
        $dateFormat = $config->getJSDateformat();
        return $dateFormat." H:i";
    }

    /**
     * Returns all unit that have a min stay on this period less than this period
     *
     * @param RM_Reservation_Period $period
     * @return array array with unit ids
     */
    public function getByMinPeriod(RM_Reservation_Period $period)
    {
        $unitIDs = array();
        $model = new RM_HourlyUnitPrices();
        $unitIDs = $model->getByMinPeriod($period);
        return $unitIDs;
    }

    /**
     * Calculates total price for one unit
     *
     * @param RM_Prices_Information $information All needed data for calculate total price
     * @param bool $byStep - if true, the result will be in array (0 => array('step' => RM_Date, 'price' => float))
     * @return float Total price
     */
    public function getTotalUnitPrice(RM_Prices_Information $information, $byStep = false)
    {
        $calculator = new RM_HourlyPrices_Calculator(
            new RM_HourlyUnitPrices,
            new RM_UnitHourlyPricesConfig,
            RM_Environment::getInstance()->getTranslation(RM_Environment::TRANSLATE_ERRORS)
        );
        return $calculator->getTotalUnitPrice($information, $byStep);
    }

    /**
     * Returns the lowest price of a unit over a given period
     *
     * @param $information RM_Prices_Information - information object,
     * contains all needed information about unit reservation:
     * unit,
     * start date,
     * end date,
     * number of persons
     * etc.
     *
     * @return float
     */
    public function getLowestUnitPrice(RM_Prices_Information $information){
        $model = new RM_HourlyUnitPrices();
        return $model->getLowestPrice($information);
    }

    /**
     * Return max prices among all units for the one default period for price manager
     * @return float
     */
    public function getTotalHighestPrice(){
        $model = new RM_HourlyUnitPrices();
        return $model->getHighestPrice();
    }

    /**
     * Returns all unit ids that have price in price range in input period
     *
     * @param float $from
     * @param float $to
     * @param RM_Reservation_Period $period
     * @return array array with unit ids
     */
    public function getByPriceRange($from, $to, RM_Reservation_Period $period = null){
        $model = new RM_HourlyUnitPrices();
        $rows = $model->getByPriceRange($from, $to, $period);

        $unitIDs = array();
        foreach ($rows as $row) {
            $unitIDs[] = $row->unit_id;
        }
        return $unitIDs;
    }

    /**
     * this method returns if the times should be collected for this price module
     *
     * @return boolean
     */
    public function getShowTime(){
        return 1;
    }

    /**
     * Returns the other information associated with this price system.
     *
     * @return array
     */
    public function getOtherInfo(){
        return null;
    }

        /**
     * this method returns a default value for people maximums (used in hospitality and other modules)
     *
     * @return boolean
     */
    public function getPeopleMaximums(){
        return 1;
    }

     /**
     * this method returns a default value for board types (un-used on hourly)
     *
     * @return null
     */
    public function getBoardTypes(){
        return null;
    }

    /**
     * returns uits that have the availability check disabled.
     * @return array
     */
    public function getUnitWithAvailabiltyCheckDisabled(){
        $model = new RM_UnitHourlyPricesConfig();
        $rows = $model->fetchUnitsByValue('availablity_check');

        $unitIDs = array();
        foreach ($rows as $row) {
            if ($row->value=="0"){
                $unitIDs[] = (array("id"=>$row->unit_id));
            }
        }
        return $unitIDs;
    }
}